# -*- coding: utf-8 -*-
# @Time : 2022/5/20 16:51
# @FileName: AccChart.py
# @Mail: lizhigen1996@aliyun.com

__author__ = 'Zhigen.li'

from datetime import datetime
import time
import sys, os
from PyQt5.QtCore import Qt, QRect, QDateTime, QDate, QPoint, QPointF, pyqtSignal, QEvent
from PyQt5.QtGui import QPainter, QCursor, QColor, QBrush, QMouseEvent, QIcon
from PyQt5.QtWidgets import QToolTip, QWidget, QApplication, QGridLayout, QGraphicsScene, \
    QGraphicsView, QSpacerItem, QSizePolicy, QMenu, QAction
from PyQt5.QtChart import QChart, QChartView, QBarSeries, QBarSet, QBarCategoryAxis, QValueAxis, QLineSeries, QAbstractBarSeries
from res.resource_rc import *
from Config.Config import CatchExceptions

class AccChart(QWidget):
    def __init__(self, data, parent):
        super(AccChart, self).__init__(parent)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")


        for i in range(len(data) - 1):
            for j in range(i + 1, len(data)):
                t1 = datetime.strptime(data[i]['date'], '%Y-%m-%d')
                t2 = datetime.strptime(data[j]['date'], '%Y-%m-%d')
                if t2 < t1:
                    data[i], data[j] = data[j], data[i]
        self.data = data[::-1]
        print(self.data)

        self.dates = []
        self.earns = []
        for d in self.data:
            self.dates.append(d['date'])
            self.earns.append(d['earn'])

        series = QBarSeries()
        self.setTempZ = QBarSet('收益记录正')
        self.setTempZ.hovered.connect(self.onBarSetClicked)
        self.setTempZ.append([x if x > 0 else 0 for x in self.earns])
        self.setTempZ.setColor(QColor('#E14745'))
        series.append(self.setTempZ)

        self.setTempF = QBarSet('收益记录负')
        self.setTempF.hovered.connect(self.onBarSetClicked)
        self.setTempF.append([x if x < 0 else 0 for x in self.earns])
        self.setTempF.setColor(QColor('#3F931F'))
        series.append(self.setTempF)

        # # 设置标签显示位置
        # series.setLabelsPosition(QAbstractBarSeries.LabelsPosition.LabelsOutsideEnd)
        # # 显示标签
        # series.setLabelsVisible(True)
        # # 设置标签颜色
        # self.setTempZ.setLabelColor(Qt.red)
        # self.setTempF.setLabelColor(Qt.green)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("历史收益记录")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        axisY = QValueAxis()
        axisY.applyNiceNumbers()
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        self.axis_x = QBarCategoryAxis()
        self.axis_x.append(self.dates)
        self.axis_x.setLabelsAngle(75)
        chart.addAxis(self.axis_x, Qt.AlignBottom)
        series.attachAxis(self.axis_x)

        # 隐藏刻度线
        self.axis_x.setGridLineVisible(False)
        axisY.setGridLineVisible(False)

        chart.legend().setVisible(True)
        # chart.legend().setAlignment(Qt.AlignBottom)

        # 设置零线
        # seriesZero = QLineSeries(chart)
        # seriesZero.setName('零刻度线')
        # chart.setAxisX(self.axis_x, seriesZero)
        # chart.setAxisY(axisY, seriesZero)
        # print(self.axis_x.max())
        # seriesZero.append(QPointF(0, 0))
        # seriesZero.append(QPointF(20, 20))
        # seriesZero.setVisible(True)
        # chart.addSeries(seriesZero)

        self.chartView = QChartView(chart)
        self.chartView.setRenderHint(QPainter.Antialiasing)
        self.gridLayout.addWidget(self.chartView, 0, 0, 1, 1)

        # 旋转
        # pScene = QGraphicsScene(self.chartView)
        # pScene.addWidget(self.chartView)
        # pView = QGraphicsView(pScene, self.chartView)
        # pView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        # pView.rotate(90)
        # self.chartView.resize(1600, 600)
        # print(pView.size())
        # self.gridLayout.addWidget(pView, 0, 0, 1, 1)

    @CatchExceptions
    def onBarSetClicked(self, status, index):
        if status:
            money = 0
            if self.sender() == self.setTempZ:
                money = str(round([x if x > 0 else 0 for x in self.earns][index], 2))
            if self.sender() == self.setTempF:
                money = str(round([x if x < 0 else 0 for x in self.earns][index], 2))

            text = '{}\n{}'.format(self.dates[index], money)
            QToolTip.showText(QCursor.pos(), text, self, self.rect(), 2000)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 初始化所有视图
    data = [{'date': '2022-04-26', 'money': 1020.3, 'earn': 20.299999999999997}, {'date': '2022-04-27', 'money': 1046.21562, 'earn': 25.915619999999997}, {'date': '2022-04-28', 'money': 1073.94033393, 'earn': 27.724713929999997}, {'date': '2022-04-29', 'money': 1079.847005766615, 'earn': 5.906671836615001}, {'date': '2022-05-05', 'money': 1081.7907303769948, 'earn': 1.9437246103799068}, {'date': '2022-05-06', 'money': 1040.682682622669, 'earn': -41.1080477543258}, {'date': '2022-05-09', 'money': 2976.2202097510685, 'earn': -64.46247287160058}, {'date': '2022-05-10', 'money': 5053.35162300221, 'earn': 77.13141325114157}, {'date': '2022-05-11', 'money': 5124.0985457242405, 'earn': 70.74692272203093}, {'date': '2022-05-12', 'money': 5175.339531181484, 'earn': 51.24098545724241}, {'date': '2022-05-13', 'money': 5157.225842822349, 'earn': -18.11368835913519}, {'date': '2022-05-16', 'money': 5114.936590911206, 'earn': -42.28925191114325}, {'date': '2022-05-17', 'money': 7176.836539252132, 'earn': 61.89994834092748}, {'date': '2022-05-18', 'money': 7114.398061360639, 'earn': -62.43847789149355}, {'date': '2022-05-19', 'money': 8039.7455991961215, 'earn': -74.65246216451787}, {'date': '2022-05-20', 'money': 8359.727474044128, 'earn': 319.98187484800565}]
    login = AccChart(data, None)
    login.resize(1000, 600)
    login.show()
    sys.exit(app.exec_())

